package cn.picclife.mwx.manager.interceptor;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.OperatorMenuInfo;

public class AuthInterceptor extends HandlerInterceptorAdapter {
	// 登陆页面
	private static final String loginPage = "/login/login.html";

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception ex)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		// session管理和登陆判断
		Boolean isMenu = false;
		String realPath = "";

		String optNo = (String) request.getSession().getAttribute("optNo");
		List<OperatorMenuInfo> menuList = (List) request.getSession().getAttribute("menuList");

		String path = request.getRequestURI();
		if (path.contains("?")) {
			realPath = path.substring(path.indexOf("/", 1) + 1, path.indexOf("?"));
		} else {
			realPath = path.substring(path.indexOf("/", 1) + 1);
		}
		if (menuList == null) {
			isMenu = false;
		} else {
			for (OperatorMenuInfo operatorMenuInfo : menuList) {
				String menuUrl = operatorMenuInfo.getMenuUrl();
				if (realPath.equals(menuUrl)) {
					isMenu = true;
					break;
				}
			}
		}
		 /*if (!isMenu) {
			String requestType = request.getHeader("x-requested-with");
			boolean isAjax = false;
			if ("XMLHttpRequest".equals(requestType)) {
				isAjax = true;
			}
			// 判断是否是ajax请求
			if (isAjax) {
				BaseResult result = new BaseResult();
				result.setRet(BaseResult.AUTH_ERROR);
				result.setMsg("您没有操作权限，请通知管理员");
				PrintWriter out = response.getWriter();
				response.setStatus(HttpStatus.BAD_REQUEST.value());
				out.print(JSON.toJSONString(result));
				out.flush();
				out.close();
			} else {
				String ctx = request.getContextPath();
				// 未登录
				PrintWriter out = response.getWriter();
				StringBuilder builder = new StringBuilder();
				builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
				builder.append("alert(\"您没有操作权限，请通知管理员\");");
				builder.append("</script>");
				out.print(builder.toString());
				out.close();
			}
			return false;
		} */
		return true;
	}
}
